package com.ruoyi.basedata.service.impl;

import java.util.List;

import com.ruoyi.basedata.domain.Phone;
import com.ruoyi.basedata.mapper.PhoneMapper;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basedata.mapper.CustomerMapper;
import com.ruoyi.basedata.domain.Customer;
import com.ruoyi.basedata.service.ICustomerService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 客户Service业务层处理
 * 
 * @author wangjin
 * @date 2024-10-24
 */
@Service
public class CustomerServiceImpl implements ICustomerService 
{
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private PhoneMapper phoneMapper;
    /**
     * 查询客户
     * 
     * @param Id 客户主键
     * @return 客户
     */
    @Override
    public Customer selectCustomerById(Integer Id)
    {
        return customerMapper.selectCustomerById(Id);
    }

    /**
     * 查询客户列表
     * 
     * @param customer 客户
     * @return 客户
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer)
    {
        return customerMapper.selectCustomerList(customer);
    }
    @Override
    public Customer selectCustomerByName(String cusName)
    {
        return customerMapper.selectCustomerByName(cusName);
    }

    @Override
    public int getCusCount() {
        return customerMapper.getCusCount();
    }

    @Override
    public AjaxResult getAllCus() {
        return AjaxResult.success(customerMapper.getAllCus());
    }

    /**
     * 新增客户
     * 
     * @param customer 客户
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCustomer(Customer customer)
    {
        String perCode ="KH"+System.currentTimeMillis();
        customer.setCusCode(perCode);
        Phone phone = new Phone();
        phone.setPerCode(perCode);
        phone.setPhone(customer.getCusPhone());
        phone.setCreateDate(customer.getCreateDate());
        phone.setCreateBy(customer.getCreateBy());
        phoneMapper.insertPhone(phone);
        return customerMapper.insertCustomer(customer);
    }

    /**
     * 修改客户
     * 
     * @param customer 客户
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCustomer(Customer customer)
    {
        Phone phone = new Phone();
        phone.setPerCode(customer.getCusCode());
        phone.setPhone(customer.getCusPhone());
        phone.setModifyBy(customer.getModifyBy());
        phone.setModifyDate(customer.getModifyDate());
        phoneMapper.updatePhone(phone);
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 批量删除客户
     * 
     * @param Ids 需要删除的客户主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByIds(Integer[] Ids)
    {
        return customerMapper.deleteCustomerByIds(Ids);
    }

    /**
     * 删除客户信息
     * 
     * @param Id 客户主键
     * @return 结果
     */
    @Override
    public int deleteCustomerById(Integer Id)
    {
        return customerMapper.deleteCustomerById(Id);
    }
}
